<?php
namespace Addons\Avatar\Controller;
use Home\Controller\AddonsController;
class AvatarController extends AddonsController{
    /* 上传图片 */
    public function upload(){
        $driver = 'Local';
        /* 上传配置 */
        $config =  array(
            'mimes'    => '', //允许上传的文件MiMe类型
            'maxSize'  => 2*1024*1024, //上传的文件大小限制 (0-不做限制)
            'exts'     => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            'autoSub'  => true, //自动子目录保存文件
            'subName'  => array('date', 'Y-m-d'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
            'rootPath' => './Uploads/Picture/', //保存根路径
            'savePath' => '', //保存路径
            'saveName' => array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
            'saveExt'  => '', //文件保存后缀，空则使用原后缀
            'replace'  => false, //存在同名是否覆盖
            'hash'     => true, //是否生成hash编码
            'callback' => false, //检测文件是否存在回调函数，如果存在返回文件信息数组
        );
        /* 返回标准数据 */
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
     
        /* 调用文件上传组件上传文件 */
        $Picture = D('Admin/Picture');
        $info = $Picture->upload(
            $_FILES,
            $config,
            $driver
        ); //TODO:上传到远程服务器

        /* 记录图片信息 */
        if($info){
            $return['status'] = 1;
            $return['pic_id'] = $info['imgFile']['id'];
            $return['s_path'] = $info['imgFile']['path'];
            $return['fullpath'] = 'http://' . $_SERVER['HTTP_HOST'] .__ROOT__.$info['imgFile']['path'];
        } else {
            $return['status'] = 0;
            $return['info']   = $Picture->getError();
        }
        return $return;
    }
    //头像上传图片处理
    public function upavatar(){
        $img = $this->upload();
        /* 返回JSON数据 */
        exit($this->ajaxReturn($img));
    }
    //裁剪方法 $w, $h, $x, $y, 
    public function cutPic($w, $h, $x, $y, $s_path, $prefix='avatar_'){
        $config = $this->getConfig();
        if($config['prefix']) {
            $prefix = $config['prefix'];
        }
        $image      = new \Think\Image();
        $avatarPath = $this->picPrefix($s_path, $prefix);
        $image->open($s_path);
        $image->crop($w, $h, $x, $y)->save($avatarPath);
    }
    //保存图片方法
    public function savePic(){
        $uid    = is_login();
        $config = $this->getConfig();
        if(empty($uid)) {
            $this->error('请登录！');
        }
        if(empty($_POST['pic_id'])) {
            $this->success('跳转中',$url);exit();
        }
        $s_path = '.' . $_POST['s_path'];
        $this->cutPic($_POST['w'], $_POST['h'], $_POST['x'], $_POST['y'], $s_path);
        if(M('member')->where(array('uid' => $uid))->save(array('avatar' => $_POST['pic_id'])) === false) {
            $this->error('系统异常！');
        }
        if($config['success_url']) {
            $url = U($config['success_url']);
        }
        $this->success('成功',$url);
    }
    //获取配置
    public function getConfig(){
        $config  =   M('Addons')->where(array('status' => 1, 'name' => 'Avatar'))->getField('config');
        if($config) {
            $config   =   json_decode($config, true);
            return $config;
        }
        return null;
    }
    //给图片文件名加前缀
    public function picPrefix($fileName, $fix){
        $pos        = strrpos($fileName, '/') + 1 ;
        $exstr      = str_split($fileName, $pos);
        $avatarPath = $exstr[0] . $fix . $exstr[1];
        return  $avatarPath;
    }

    public function test(){
        echo 'Test A';
    }
}
